
#ifndef DIALOGLISTWIDGET_H
#define DIALOGLISTWIDGET_H

#include <BScrollArea>

#include "DialogListItem.h"

class DialogListWidget : public BScrollArea
{
	friend class DialogListItem;

public:
	DialogListWidget(BWidget* parent = 0);
	DialogListWidget(const BString& title, BWidget* parent = 0);
	~DialogListWidget();

	bool addItem(DialogListItem* item);
	DialogListItem* addItem(const BString& text);
	DialogListItem* addItem(BIcon* icon, const BString& text);
	DialogListItem* addItem(BImage* image, const BString& text);

	bool insertItem(int pos, DialogListItem* item);
	DialogListItem* insertItem(int pos, const BString& text);
	DialogListItem* insertItem(int pos, BIcon* icon, const BString&);
	DialogListItem* insertItem(int pos, BImage* image, const BString&);

	bool removeItem(DialogListItem* item);
	bool removeItem(const BString& text);
	bool removeItem(int index);

	void setSelectMode(SelectMode selectMode);
	SelectMode selectMode() const;

	void setIconSize(const BSize& iconSize);
	const BSize& iconSize() const;

	bool setItemSelected(DialogListItem* item, bool selected);

	int selectedItemCount() const;
	DialogListItem* selectedItem(int index = 0);
	const DialogListItem* selectedItem(int index = 0) const;

	void clearItems();
	bool clearSelection();

	int itemCount() const;
	DialogListItem* item(int index);
	const DialogListItem* item(int index) const;

	DialogListItem* item(const BString& text);
	const DialogListItem* item(const BString& text) const;

	DialogListItem* itemAt(const BPoint& point);
	const DialogListItem* itemAt(const BPoint& point) const;

	DialogListItem* itemOfAsset(const BValue& asset);
	const DialogListItem* itemOfAsset(const BValue& asset) const;

	void scrollTo(DialogListItem* item, Align align = Align_None);
	bool sort(int dir = 1);

protected:
	void dirtyItem(DialogListItem* item);
	void freshArea();
	void freshVisualItems();

protected:
	virtual void scrolling(Orientation orientation);

protected:
	virtual void resizeEvent(const BEvent& event);

	virtual void styleEvent(const BEvent& event);
	virtual void freshEvent(const BEvent& event);
	virtual void paintEvent(const BEvent& event);

	virtual void mouseEnterEvent(const BMouseEvent& mouseEvent);
	virtual void mouseLeaveEvent(const BMouseEvent& mouseEvent);

	virtual void mousePressEvent(const BMouseEvent& mouseEvent);
	virtual void mouseReleaseEvent(const BMouseEvent& mouseEvent);
	virtual void mouseMoveEvent(const BMouseEvent& mouseEvent);

	virtual void mouseClickEvent(const BMouseEvent& mouseEvent);
	virtual void mouseDoubleClickEvent(const BMouseEvent& mouseEvent);

private:
	int					_spacing;
	int					_showItemBegin;
	int					_showItemEnd;
	SelectMode			_selectMode;
	BSize				_iconSize;
	bool				_dirty;
	BSize				_minAreaSize;
	DialogListItem*		_pressedItem;
	DialogListItem*		_hoveredItem;

	BArray<DialogListItem*>			_selectedItems;
	BList<BHolder<DialogListItem>>	_items;
};

#endif
